package com.chapter1;

public class test7 {
    public static void main(String[] args) {
        int[] weights = {1,2,3,4,5,6,7,8,9,10};
        int D = 5;
        System.out.println(solution(weights,D));
    }

    public static int solution(int[] weights, int D){
        int ans = 0;
        int max = 0;
        int length = weights.length;
        for(int i = 0; i < length; i++){
            ans += weights[i];
            if(max < weights[i]){
                max = weights[i];
            }
        }
        ans = Math.max(ans / D, max);
        while(true){
            int sum = 0;
            int day = 1;
            for(int i = 0; i < length; i++){
                if(sum + weights[i] <= ans){
                    sum += weights[i];
                } else {
                    sum = weights[i];
                    day++;
                }
            }
            if(day <= D){
                return ans;
            } else {
                ans++;
            }
        }
    }

}
